var param = JSON.parse(localStorage.getItem('param'));
var type = param.type;
var purAd = '';
$(function(){
	init();
	initButton();
});
//初始化页面
function init(){
	$.ajax({
		type : "get",
		url : server_url
				+ 'rest/tea/product/getTeaProductList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data : {
			token : token,
			type:type
		},
		success : function(result) {
			if (result.retCode == '0000') {
				showList(result.data);
			} 
		}
	});

}
//初始化绑定事件
function initButton(){
	$("body").on('click','.pro-item',function(){
		var param = {};
		param.productId = $(this).attr('productid');
		param.type = $(this).attr('type');
		//跳转
		localStorage.setItem('param',JSON.stringify(param));
		window.location=server_url+"proDetail.do";
	});
	//绑定销量查询按钮
	$('.sell').on('click',function(){
		$('.price-item').removeAttr("style");
		$('.select-item').removeAttr("style");
		$('.sell-item').attr('style',"color:#01cb72");
		$('.hotBank-list-ico').parents('.bank_xl').hide();
		searchProList("tea_sell_num","desc");
	});
	//绑定价格查询按钮
	$('.price').on('click',function(){
		$('.sell-item').removeAttr("style");
		$('.select-item').removeAttr("style");
		$('.price-item').attr('style',"color:#01cb72");
		$('.hotBank-list-ico').parents('.bank_xl').hide();
		var h = $(this).children().html();
		if(h=="价格由低到高"){
			$(this).children().html('价格由高到低');
			searchProList("tea_price","desc");
		}else{
			$(this).children().html('价格由低到高');
			searchProList("tea_price","asc");
		}
	});
	//选中某一项（预购或者现购）
	$(".hotBank-list-ico").bind('click', function() {
		$(this).parents('.bank_xl').siblings('.position-rel').children('.form-control').removeAttr('placeholder');
		$(this).parents('.bank_xl').siblings('.position-rel').children('.chose_bank').children('span').html($(this).attr('title'));
		$(this).parents('.bank_xl').hide();
		if($(this).attr('title')=="现购"){
			purAd = '0';
		}else if($(this).attr('title')=="预购"){
			purAd = '1';
		}
		searchProList();
	});
	//弹出下拉框
	$(".chose_bank").click(function() {
		$('.sell-item').removeAttr("style");
		$('.price-item').removeAttr("style");
		$('.select-item').attr('style',"color:#01cb72");
		
		var select = $(this);
		var selects = $('.chose_bank');
		$.each(selects,function(i,n){
			if(!($(n).attr('id')==select.attr('id'))){
				$(this).parent().siblings('.bank_xl').hide();
			}
		});
		$(this).parent().siblings('.bank_xl').show();
	});
}
function showList(productList){
	var str = '';
	$.each(productList,function(i,n){
		console.log(n);
		var teaPurAd = n.teaPurAd;
		var purStr;
		if(teaPurAd=='0'){
			//现购
			purStr = '<span class="fl green-box	">现购中</span>';
		}else{
			//预购
			purStr = '<span class="fl red-box	">预购中</span>';
		}
		str += '<div class="fl pro-item" type="'+n.cateid+'" productid="'+n.productid+'"><img src="'+n.pic+'" alt="" /><div class="name-box clear">'+purStr+'<p class="fr normap">'+n.productname+'</p></div><div class="num-box clear"><div class="fl">￥<span>'+n.priceretailguide+'</span></div><p class="fr">已售'+n.teaSellNum+'件</p></div></div>';
	});
	$('.pro-list').html(str);
}
function searchProList(column,sort){
	$.ajax({
		type : "post",
		url : server_url
				+ 'rest/tea/product/getTeaProductList.do?ran=' + Math.random(),
		dataType : 'json',
		async : true,
		data :{
			token : token,
			column:column,
			sort:sort,
			type:type,
			purAd:purAd
		},
		success : function(result) {
			console.log(result);
			if (result.retCode == '0000') {
				showList(result.data);
			}else{
				showErrorMsg(result.data);
			}
		}
	});
}